<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Color_model short summary.
 *
 * Color_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Color_model extends CI_Model
{
    public $color_id;
    public $title;
    public $colorkey;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    //插入新颜色
    public function insert_color($colorModel)
    {
        return $this->db->insert('color',$colorModel);
    }

    //由颜色id 获取颜色
    public function get_color_by_id($color_id)
    {
        return $this->db->select()->from('color')->where(array('color_id'=>$color_id))->get()->row();
    }

    //获取所有颜色
    public function get_allcolor()
    {
        return $this->db->select()->from('color')->get()->result();
    }

    public function get_title($colorid)
    {
        return $this->db->select('title')->from('color')->where(array('color_id'=>$colorid))->get()->row();
    }



    //更新颜色
    public function edit_color($colorModel)
    {

        return  $this->db->update('color',$colorModel,array('color_id'=>$colorModel->color_id));

    }

    //删除颜色
    public function del_color_byid($color_id)
    {
         $this->db->trans_begin();
         $this->db->delete('color',array('color_id'=>$color_id));
         $this->del_color_relation_data($color_id);
         if ($this->db->trans_status() === FALSE)
         {
             $this->db->trans_rollback();
             return 0;
         }
         else
         {
             $this->db->trans_commit();
             return 1;
         }
    }

    public function del_color_relation_data($color_id)
    {
        //删除库存表中的库存
        $sql="delete from goods_inventory where goods_color_id =$color_id";
        $this->db->query($sql);
        //删除商品颜色标goods_color
        $this->db->delete('goods_color',array('color_id'=>$color_id));

        //更新商品图片表
        $update_goods_img_sql="update goods_img set goods_color_id=0 where goods_color_id=$color_id";
        $this->db->query($update_goods_img_sql);
    }

    //批量删除颜色
    public function batchdel_color($color_ids)
    {
        $this->db->trans_begin();
        foreach($color_ids as $coloriditem)
        {
            $this->db->delete('color',array('color_id'=>$coloriditem));
            $this->del_color_relation_data($coloriditem);
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }



    /**
     * @param mixed $cpage
     * @param mixed $pagesize
     * @param mixed $files
     * @param mixed $tabel
     * @param mixed $order
     */
    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id ";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }

    // 获取全部尺码以及 商品尺码 left join 查询
    public function get_color_goods_color($goodid)
    {
        $sql="select c.color_id as colorid,c.title as title,c.colorkey as colorkey,g.goods_color_id  from color c LEFT JOIN goods_color g on c.color_id=g.color_id and g.goods_id=$goodid";
        return $this->db->query($sql)->result_array();
    }
}